package munyorderweb_Android;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import munyorderweb_DB.DB_manager;

public class Android_CServlet extends HttpServlet
{
	//Androidへカテゴリーテーブルの情報を送信する
	private final String SQL_CATEGORY = "select * from category order by category_no";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		A_C(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		A_C(req,resp);
	}

	protected void A_C(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/plain; charset=UTF-8");
		PrintWriter out = resp.getWriter();
		//DB接続
		Connection con = DB_manager.getConnection();

		try {
			Statement stm = con.createStatement();
			//取得した情報を一行の文字列に結合
			ResultSet rs = stm.executeQuery(SQL_CATEGORY);
			while (rs.next()) {
				out.print(rs.getInt("category_no"));
				out.print(",'");
				out.print(rs.getString("category_name"));
				out.print("',");
				out.print(rs.getInt("category_use"));
				out.print("\n");
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		try{
			//DB接続の終了
			con.close();
		}catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
}
